Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add friendly_id gem and update node paths on changing title #600

Merged
merged 7 commits into from
Aug 12, 2016

Conversation

ananyo2012
Copy link
Member

@ananyo2012 ananyo2012 commented Jul 1, 2016

Make sure these boxes are checked before your pull request is ready to be reviewed and merged. Thanks!

  • tests pass -- rake test
  • code is in uniquely-named feature branch, and has been rebased on top of latest master (especially if you've been asked to make additional changes)
  • pull request are descriptively named
  • if possible, multiple commits squashed if they're smaller changes
  • reviewed/confirmed/tested by another contributor or maintainer
  • schema.rb.example has been updated if any database migrations were added

Important

To make use of the changes we need to populate the new slug column for the existing data in the database. So we have to run this query from the rails console.

DrupalNode.find_each(&:save)

Thanks!

@ananyo2012
Copy link
Member Author

This solves the issue of updating node titles when titles are changed. Since wiki pages, notes, questions, and maps come under the same umbrella model of drupal_node with varying paths so I have continued using node paths as it is for nodes. Just adding a update_path method which will be executed before updating the node. This updates the node paths when title are changed.

@ananyo2012
Copy link
Member Author

ananyo2012 commented Jul 1, 2016

Now coming to the role of friendly_id here. Once node paths are updated older path becomes obsolete. So people will land up with 404's if trying to visit the nodes will old urls. Here comes friendly_id's :history module which saves from any 404's and it is possible to redirect old url's to new ones.

end
# def slug
# self.path.split('/').last
# end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to comment this due to new slug column introduced in the node table.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are slug and path different?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the present context path is the full url of a node like /notes/used/07-06-2016/some-test-url while slug is only the parameterized version of title like some-test-url

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just delete this method, sound good?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah now we can remove this.

@ananyo2012
Copy link
Member Author

@jywarren Please review these changes once you are back. I have added tests and also updated previous tests so that these new changes stand right. I have also left comments in the code that will make it easier to follow.

Thanks!

@ananyo2012
Copy link
Member Author

I haven't updated the sqlite and schema files for any of these changes. I will do it with a follow up PR once you merge in both my PR's

@@ -142,6 +138,7 @@ def update
i.vid = @revision.vid
i.save
end
@node.title = @revision.title
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solves #7

end

def down
remove_index :node, :slug
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just two spaces, please, thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jywarren Ah overlooked it. Updated.

@jywarren
Copy link
Member

jywarren commented Jul 6, 2016

Ah, so we'd originally decided that we should not ever change the slug once its saved, as that will break any previous links, and for wiki pages, it would move the wiki page itself. So I think we must ask around if this new behavior is OK (on the dev list, maybe, and also @ebarry)? Just to be clear, it would:

  • change slugs to match title when title is updated
  • keep old paths (urls) active and 301-redirect them to new path

What happens for wiki pages? What if you move a wiki page and then want to make a new wiki page where the old one was? What if you want to change it back to the original title but the redirect record exists? If you retitle it twice -- does it doubly redirect?

In general, I like the simpler find instead of find_by_slug methods, but I'm worried about the redundancy of the slug and path columns. If we do choose to go forward here (and I appreciate the effort!) I'd like to write a bit of documentation at the top of the drupal_node file to make it clear to other developers.

@ananyo2012 ananyo2012 force-pushed the friendly_id branch 2 times, most recently from 43b7584 to c625c3f Compare July 6, 2016 19:29
@ananyo2012
Copy link
Member Author

ananyo2012 commented Jul 6, 2016

@jywarren I have made some changes to the code. Please review. Now the answers to your questions. Friendly_id has its own system to handle duplicate slugs. So all slugs are unique and no two slugs are the same. So if there is already a slug named some some-test-url then if there is a updated node having same title as Some test url the slug would be some-test-url--2 i.e. an index number with a separator is appended to the url.

  • If we change the title of wiki page and then create an wiki with the title similar to the old title of the previous wiki a new slug will be assigned which will be unique and different from the slug of the old url of the previous wiki according to the format stated above
  • If we want to change the title of a wiki again to its original title and no similar title exists then it will just use the original url
  • But if we want to change the title of the wiki to its original title but there already exists another wiki with the old title then a new unique url will be generated as per the format stated above.

So using friendly_id ensures that no wiki pages are lost and also the slug generated is unique and older urls are redirected back to the newer urls.

@ananyo2012
Copy link
Member Author

ananyo2012 commented Jul 6, 2016

@jywarren I will add some more tests to cover the cases you mentioned.

@ananyo2012
Copy link
Member Author

Well we could use just friendly_id slugs for node paths as friendly_id itself handles paths but I didn't want to completely switch over to friendly_id as it would involve changing a lot of code and the current path column had to be discarded. So to keep the best of both worlds so that node paths update as well as old urls are not lost I used the current path column along with friendly_id. To use the friendly_id :history module we need to have a slug column.

@jywarren
Copy link
Member

jywarren commented Jul 8, 2016

Ah, but just to be clear, sorry -- if you have a page like /wiki/barnraising which you then change to /wiki/barnraising-2015, and then want to make a new page called simply /wiki/barnraising again, would the old URL be available again to make this new page? Or would the old URL persist and redirect to the new /wiki/barnraising-2015?

Second, do I understand correctly then, that node.path is used as the primary path, and node.slug is used in some way for the history -- that is, preserving old URLs?

Thanks, this is a bit subtle and we need to have a clear understanding -- and to ensure people can get the URL they need on a wiki page. Note that for research notes the URL must simply be permanent for linking to (and exactly what the URL becomes is not that important-- it can have "-2" at the end with no problem), but for wikis, having a "good" or recognizable short URL is important too, and "losing" the ability to put a page at a well-known URL is more serious.

@ananyo2012
Copy link
Member Author

ananyo2012 commented Jul 8, 2016

If you have a page like /wiki/barnraising which you then change to
/wiki/barnraising-2015, and then want to make a new page called simply
/wiki/barnraising again, the old url won't be used instead a unique url
like /wiki/barnraising--2.

Yes its somewhat like that. The node.path is the primary path and the slug
is used by the history module.

On Fri, Jul 8, 2016 at 10:13 AM, Jeffrey Warren notifications@github.com
wrote:

Ah, but just to be clear, sorry -- if you have a page like
/wiki/barnraising which you then change to /wiki/barnraising-2015, and
then want to make a new page called simply /wiki/barnraising again, would
the old URL be available again to make this new page? Or would the old URL
persist and redirect to the new /wiki/barnraising-2015?

Second, do I understand correctly then, that node.path is used as the
primary path, and node.slug is used in some way for the history -- that
is, preserving old URLs?

Thanks, this is a bit subtle and we need to have a clear understanding --
and to ensure people can get the URL they need on a wiki page. Note that
for research notes the URL must simply be permanent for linking to (and
exactly what the URL becomes is not that important-- it can have "-2" at
the end with no problem), but for wikis, having a "good" or recognizable
short URL is important too, and "losing" the ability to put a page at a
well-known URL is more serious.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#600 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AKACR308tcl8r_uBk9FuFHPsGtzsRJsdks5qTdVvgaJpZM4JDL2B
.

@jywarren
Copy link
Member

jywarren commented Jul 8, 2016

Ah, hmmm. So you wouldn't be able to use /wiki/barnraising anymore? I think
we have to think of a way around that if we're going to incorporate this
code. @ebarry?
On Jul 8, 2016 10:30 AM, "Ananya Maiti" notifications@github.com wrote:

if you have a page like /wiki/barnraising which you then change to
/wiki/barnraising-2015, and then want to make a new page called simply
/wiki/barnraising again, the old url won't be used instead a unique url
like /wiki/barnraising--2.

Yes its somewhat like that. The node.path is the primary path and the slug
is used by the history module.

On Fri, Jul 8, 2016 at 10:13 AM, Jeffrey Warren notifications@github.com
wrote:

Ah, but just to be clear, sorry -- if you have a page like
/wiki/barnraising which you then change to /wiki/barnraising-2015, and
then want to make a new page called simply /wiki/barnraising again, would
the old URL be available again to make this new page? Or would the old
URL
persist and redirect to the new /wiki/barnraising-2015?

Second, do I understand correctly then, that node.path is used as the
primary path, and node.slug is used in some way for the history -- that
is, preserving old URLs?

Thanks, this is a bit subtle and we need to have a clear understanding --
and to ensure people can get the URL they need on a wiki page. Note that
for research notes the URL must simply be permanent for linking to (and
exactly what the URL becomes is not that important-- it can have "-2" at
the end with no problem), but for wikis, having a "good" or recognizable
short URL is important too, and "losing" the ability to put a page at a
well-known URL is more serious.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#600 (comment),
or mute
the thread
<
https://github.com/notifications/unsubscribe/AKACR308tcl8r_uBk9FuFHPsGtzsRJsdks5qTdVvgaJpZM4JDL2B

.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#600 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AABfJ8-MnFGzH4hRqU1X0X3FDASccyumks5qTokpgaJpZM4JDL2B
.

@ananyo2012
Copy link
Member Author

ananyo2012 commented Jul 8, 2016

I am trying to figure out how to reuse the old urls. But do we really need
it? Since in the present situation too, a new wiki page with the same url
can't be made

On Fri, Jul 8, 2016 at 11:00 PM, Ananyo Maiti ananyoevo@gmail.com wrote:

if you have a page like /wiki/barnraising which you then change to
/wiki/barnraising-2015, and then want to make a new page called simply
/wiki/barnraising again, the old url won't be used instead a unique url
like /wiki/barnraising--2.

Yes its somewhat like that. The node.path is the primary path and the slug
is used by the history module.

On Fri, Jul 8, 2016 at 10:13 AM, Jeffrey Warren notifications@github.com
wrote:

Ah, but just to be clear, sorry -- if you have a page like
/wiki/barnraising which you then change to /wiki/barnraising-2015, and
then want to make a new page called simply /wiki/barnraising again,
would the old URL be available again to make this new page? Or would the
old URL persist and redirect to the new /wiki/barnraising-2015?

Second, do I understand correctly then, that node.path is used as the
primary path, and node.slug is used in some way for the history -- that
is, preserving old URLs?

Thanks, this is a bit subtle and we need to have a clear understanding --
and to ensure people can get the URL they need on a wiki page. Note that
for research notes the URL must simply be permanent for linking to (and
exactly what the URL becomes is not that important-- it can have "-2" at
the end with no problem), but for wikis, having a "good" or recognizable
short URL is important too, and "losing" the ability to put a page at a
well-known URL is more serious.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#600 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AKACR308tcl8r_uBk9FuFHPsGtzsRJsdks5qTdVvgaJpZM4JDL2B
.

@ananyo2012
Copy link
Member Author

Currently friendly_id doesn't support this but I saw a PR
norman/friendly_id#663 where this was mentioned. So
there is a possible solution. Though I am not yet convinced why to reuse
the old urls as it is not so even in the present state. But it would be
great to know your views on it.

On Fri, Jul 8, 2016 at 11:13 PM, Ananyo Maiti ananyoevo@gmail.com wrote:

I am trying to figure out how to reuse the old urls. But do we really need
it? Since in the present situation too a new wiki page with the same url
can't be made

On Fri, Jul 8, 2016 at 11:00 PM, Ananyo Maiti ananyoevo@gmail.com wrote:

if you have a page like /wiki/barnraising which you then change to
/wiki/barnraising-2015, and then want to make a new page called simply
/wiki/barnraising again, the old url won't be used instead a unique url
like /wiki/barnraising--2.

Yes its somewhat like that. The node.path is the primary path and the
slug is used by the history module.

On Fri, Jul 8, 2016 at 10:13 AM, Jeffrey Warren <notifications@github.com

wrote:

Ah, but just to be clear, sorry -- if you have a page like
/wiki/barnraising which you then change to /wiki/barnraising-2015, and
then want to make a new page called simply /wiki/barnraising again,
would the old URL be available again to make this new page? Or would the
old URL persist and redirect to the new /wiki/barnraising-2015?

Second, do I understand correctly then, that node.path is used as the
primary path, and node.slug is used in some way for the history -- that
is, preserving old URLs?

Thanks, this is a bit subtle and we need to have a clear understanding
-- and to ensure people can get the URL they need on a wiki page. Note that
for research notes the URL must simply be permanent for linking to (and
exactly what the URL becomes is not that important-- it can have "-2" at
the end with no problem), but for wikis, having a "good" or recognizable
short URL is important too, and "losing" the ability to put a page at a
well-known URL is more serious.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#600 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AKACR308tcl8r_uBk9FuFHPsGtzsRJsdks5qTdVvgaJpZM4JDL2B
.

@jywarren
Copy link
Member

Hi, @ebarry and @steviepubliclab - would like your input here on if and why it's important to be able to preserver old URLs in the (not common) case that:

  1. a page is moved, (like for example /wiki/barnraising to /wiki/barnraising-2016)
  2. a new page is created with the title "Barnraising" with the intended (new or resuscitated) URL /wiki/barnraising)

Ananyo's done some great work here, but as it stands, it would make step 2 above impossible -- the /wiki/barnraising URL would always be locked to the renamed/moved original page, and redirected to it.

If that's OK, then I can merge this, but it's not the current behavior, and I thought we'd like to retain more flexibility over URLs.

@jywarren
Copy link
Member

I think both Liz and Stevie are off today. I'll try to get their input on Monday; thanks for your patience! We just had the Regional Barnraising event in California this past weekend, so folks are still catching up (and catching their breath!)

@ananyo2012
Copy link
Member Author

ananyo2012 commented Jul 16, 2016

No problem. Waiting for the reviews. @jywarren In the mean time I will add tests for the cases you pointed out. I am quite late in my schedule. Trying to manage it.

@steviepubliclab
Copy link
Contributor

Hello! Thanks for working on this and pinging me in.

I have a couple thoughts on the URL/Redirect idea. I like the idea that the titles/urls would match if a title is changed, also like that there would be a redirect from previous URLs to the new page, but I'm worried about a situation where someone would want to make a new wiki with an old name. If there was a way to override the redirect in creating a new page, I think it's a great move.

@ananyo2012
Copy link
Member Author

ananyo2012 commented Jul 18, 2016

@steviepubliclab As I have already discussed if we want to make a new wiki with a old name an url with an unique identifier will be used such as /wiki/barnraising--2, where /wiki/barnraising is the old title. There is currently no way in friendly_id to override a old url if it is used in a new item. But there is a method mentioned in norman/friendly_id#663, but that will require making changes in the gem itself.

@jywarren
Copy link
Member

Is there any way to delete a redirect record? Or is that what the norman
mod does?

On Mon, Jul 18, 2016 at 11:54 AM, Ananya Maiti notifications@github.com
wrote:

@steviepubliclab https://github.com/steviepubliclab As I have already
discussed if we want to make a new wiki with a old name a url with an
unique identifier will be used such /wiki/barnraising--2 where
/wiki/barnraising is the old title. There is currently no way in
friendly_id to override a old url if it is used in a new item. But there is
a method mentioned in norman/friendly_id#663
norman/friendly_id#663, but that will require
making changes in the gem itself.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#600 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AABfJ2PaHhbLYp70yJzchue75W5fROg7ks5qW6_RgaJpZM4JDL2B
.

@ananyo2012 ananyo2012 force-pushed the friendly_id branch 2 times, most recently from 02a8bff to c968e35 Compare July 22, 2016 02:09
@ananyo2012
Copy link
Member Author

@jywarren Following your suggestion I found a solution for it. We can reuse the old slugs for new wiki pages if we delete the old slugs on new wiki creation. This way it works as expected. Now new wiki pages can use the old urls of another wiki. I have added some tests to justify it. Please have a look.

@jywarren
Copy link
Member

Oh i'm so excited! I've been eager to merge this. Just to confirm, the old
urls are redirected until a new wiki tries to use the same URL, right?

On Thu, Jul 21, 2016 at 10:13 PM, Ananya Maiti notifications@github.com
wrote:

@jywarren https://github.com/jywarren Following your suggestion I found
a solution for it. We can reuse the old slugs for new wiki pages if we
delete the old slugs on new wiki creation. This way it works as expected.
Now new wiki pages can use the old urls of another wiki. I have added some
tests to justify it. Please have a look.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#600 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AABfJ6PVyxevp--IhHICgDU-Bik1II6sks5qYCcxgaJpZM4JDL2B
.

@ananyo2012
Copy link
Member Author

ananyo2012 commented Aug 1, 2016

Ok I have thought of something so that we won't have to change the format of the urls. I will be making an update to this PR soon.

@jywarren
Copy link
Member

jywarren commented Aug 1, 2016

Thanks, Ananyo -- I'm happy to take a close look and offer input. I
appreciate your attention to this!

On Aug 1, 2016 6:26 PM, "Ananya Maiti" notifications@github.com wrote:

Ok I have thought of something so that we won't have to change the format
of the urls. I will making an update to this PR soon.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#600 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AABfJ7Nt6ifhvIZOx53a01u-q7rJVLb5ks5qbnKkgaJpZM4JDL2B
.

@@ -90,8 +90,10 @@ def raw

def show
if params[:author] && params[:date]
@node = DrupalNode.where(path: "/notes/#{params[:author]}/#{params[:date]}/#{params[:id]}").first
finder = "#{params[:author]} #{params[:date]} #{params[:id]}".parameterize
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jywarren This keeps the url format intact. I am just creating a finder string from the parameters that finds the appropriate node using the friendly_id slug. Thus we can use best of both worlds! Not modifying the url format as well as using the friendly_id for old url redirects. while updating the url as the title changes.

@ananyo2012
Copy link
Member Author

@jywarren I have solved the problem discussed earlier. Also rebased on the latest master. Please review the changes.

@jywarren
Copy link
Member

jywarren commented Aug 9, 2016

Wow, exciting; checking this out now.

slug.blank? || title_changed?
end

def pretty_url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be called "friendly_id_generator" or "friendly_id_adaptor" as the string it returns is not actually used as a URL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or friendly_id_string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

friendly_id_string sounds perfect

@jywarren
Copy link
Member

jywarren commented Aug 9, 2016

This looks great. I just added a couple small requests and we're basically done. Thanks!

@ananyo2012
Copy link
Member Author

ananyo2012 commented Aug 9, 2016

@jywarren I have updated the PR as you requested. Also updated the schema file.Please review.

Create migrations for slug column and friendly_id_slug table
Add update method to update node path

Modify show action in controllers for finding object by slugs
Create friendly_id_slugs fixture for history module
Add integration tests for corresponding changes
…rmat intact

Define down method for rename long column migration
@jywarren
Copy link
Member

Running tests... this is a huge update, Ananyo!

@jywarren
Copy link
Member

We're going to have to watch out extra carefully for any issues that crop up, but your tests look quite thorough.

Bundle updated, migrations run... tests running...

All good! Wow. Merging...

@jywarren jywarren merged commit ae9b01e into publiclab:master Aug 12, 2016
@jywarren
Copy link
Member

Congratulations, this was a big one. Thanks for sticking with it and for all your hard work on it!

@ananyo2012
Copy link
Member Author

@jywarren Thanks for patience and time Jeff. You also stuck to it till the end. Once again to remind you you need to run this command in the rails console to initialize all the slug column of the nodes.

DrupalNode.find_each(&:save)

I will also look at any issues that may creep up after this and solve it asap. Thanks a lot Jeff!

@jywarren
Copy link
Member

Oh, I should've mentioned, you can add that command to the migration--
could you add a follow-up migration with that command and it'll run when I
next publish to the live server? It can contain just that line.

On Aug 12, 2016 3:15 PM, "Ananya Maiti" notifications@github.com wrote:

@jywarren https://github.com/jywarren Thanks for patience and time
Jeff. You also stuck to it till the end. Once again to remind you you need
to run this command in the rails console to initialize all the slug column
of the nodes.

DrupalNode.find_each(&:save)

I will also look at any issues that may creep up after this and solve it
asap. Thanks a lot Jeff!


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#600 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AABfJ9xTRA-H9IQD42nvANhyIBWAFItuks5qfH_pgaJpZM4JDL2B
.

@ananyo2012 ananyo2012 deleted the friendly_id branch August 16, 2016 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants